<template>
  <div>
      <el-form :model="searchForm" inline label-width="80px">
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="datetimerange"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" clearable placeholder="选择状态">
            <el-option label="可用" :value="1"/>
            <el-option label="禁用" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList">搜索</el-button>
          <el-button @click="clearForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-group" v-if="loginInfo.roleId === 4">
        <el-button type="primary" size="mini" @click="openDialog()">添加</el-button>
      </div>
      <el-table :data="dataList" border>
        <el-table-column prop="id" label="编号" align="center"/>
        <el-table-column prop="type" label="名称" align="center">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.type === 1">保养</el-tag>
            <el-tag type="warning" v-else>维修</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="content" show-overflow-tooltip label="内容" align="center"/>
        <el-table-column prop="driverName" label="操作人" align="center"/>
        <el-table-column prop="startTime" label="开始时间" align="center"/>
        <el-table-column prop="endTime" label="结束时间" align="center"/>
        <el-table-column prop="money" label="消费(￥)" align="center"/>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <template v-if="loginInfo.roleId === 1">
              <el-button v-if="scope.row.status === 1" size="mini" type="success" @click="changeStatus(scope.row.id,2)">可用</el-button>
              <el-button v-else size="mini" type="danger" @click="changeStatus(scope.row.id,1)">禁用</el-button>
            </template>
            <template v-else>
              <el-tag v-if="scope.row.status === 1" size="mini" type="success">可用</el-tag>
              <el-tag v-else size="mini" type="danger">禁用</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160px">
          <template slot-scope="scope">
            <el-button size="mini" v-if="loginInfo.roleId === 4" @click="openDialog(scope.row.id)">修改</el-button>
            <el-button size="mini" v-else @click="deleteInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" @current-change="currentChange" @size-change="sizeChange" layout="total,sizes,prev,pager,next,jumper" :total="pageInfo.total"/>
    <record-update ref="recordUpdate" @closeDialog="getDataList"/>
  </div>
</template>

<script>
import RecordUpdate from '@/views/record/RecordUpdate.vue'

const initData = {
  timeRange: [],
  status: '',
  type: 1
}
export default {
  name: 'RecordList',
  components: { RecordUpdate },
  data () {
    return {
      loginInfo: {},
      searchForm: {
        ...initData
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    getDataList () {
      const timeRange = this.searchForm.timeRange.join()
      this.$http.get('record/page', { ...this.searchForm, timeRange, ...this.pageInfo }).then(data => {
        if (data) {
          this.dataList = [...data.rows]
          this.pageInfo.total = data.count
        }
      }).catch(() => {})
    },
    currentChange (page) {
      this.pageInfo.page = page
      this.getDataList()
    },
    sizeChange (limit) {
      this.pageInfo.limit = limit
      this.getDataList()
    },
    clearForm () {
      this.searchForm = { ...initData }
      this.pageInfo = {
        page: 1,
        limit: 5,
        total: 0
      }
      this.getDataList()
    },
    changeStatus (id, status) {
      this.$confirm(`确定更改编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的状态为${status === 1 ? '<span style="color: green;font-weight: bold;">可用</span>' : '<span style="color: red;font-weight: bold;">禁用</span>'}吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.post('record/status', { id, status }).then(() => {
          this.$message.success('更新状态成功')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号<span style="font-weight: bold;color: #66b1ff">[${id}]</span>的数据吗?`, '询问', { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.delete(`record/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        }).catch(() => {})
      }).catch(() => {})
    },
    openDialog (id) {
      this.$refs.recordUpdate.init(this.searchForm.type, id)
    },
    setType (route) {
      this.searchForm.type = route.meta.type
      this.getDataList()
      this.$http.get('userinfo/info').then(data => {
        this.loginInfo = { ...data }
      }).catch(() => {})
    }
  },
  watch: {
    $route (to) {
      this.setType(to)
    }
  },
  created () {
    this.setType(this.$route)
  }
}
</script>

<style scoped>

</style>
